
//String 类 里方法的初使用

import java.util.Arrays;

class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {
    public static void compare(int ret){
        if(ret > 0){
            System.out.println("str1 > str5");
        }else if(ret == 0){
            System.out.println("str1 = str5");
        }else {
            System.out.println("str1 < str5");
        }
    }

    public static void main(String[] args) {
        //字符串构造 三种常用的方法
        String str1 = "hello";
        System.out.println("str1:" + str1);

        String str2 = new String("world");
        System.out.println("str2:" + str2);

        char[] chars = {'h','e','h','e'};
        String str3 = new String(chars);
        System.out.println("str3:" + str3);

        String str4 = "hello";
        String str5 = "Hello";

        System.out.println("==========================");

        //比较字符串是否相等
        System.out.println("equals:" + str1.equals(str4));

        //忽略大小写比较字符串是否相等
        System.out.println("equalsIgnoreCase:" + str1.equalsIgnoreCase(str5));

        System.out.println("==========================");

        //比较字符串的大小
        int ret1 = str1.compareTo(str5);
        compare(ret1);

        //忽略大小写比较字符串的大小
        int ret2 = str1.compareToIgnoreCase(str5);
        compare(ret2);

        System.out.println("==========================");

        //查找字符
        //遍历字符串
        for (int i = 0; i < str1.length(); i++) {
            char ch = str1.charAt(i);
            System.out.print(ch+ "  ");
        }
        System.out.println();

        //从头开始查找字符，遇到就结束，返回遇到的第一个字符的下标
        System.out.println(str1.indexOf('l'));

        //指定位置查找字符，遇到就结束，返回遇到的第一个字符的下标
        System.out.println(str1.indexOf('l',3));

        //从头开始查找字符串
        System.out.println(str1.indexOf("llo"));

        //从指定位置开始查找字符串
        System.out.println(str1.indexOf("llo",3));

        System.out.println("==========================");

        //从后开始查找字符，遇到就结束，返回遇到的第一个字符的下标
        System.out.println(str1.lastIndexOf('l'));
        //其他同上

        System.out.println("==========================");

        //转换
        //其他类型基本均可转换成字符串，以下不在举例
        String str10 = String.valueOf(123);
        System.out.println(str10);
        //类也可转字符串,需要重写toString
        String str11 = String.valueOf(new Student("Mike",15));
        System.out.println(str11);

        System.out.println("==========================");

        //字符串转其他
        int a = Integer.parseInt("123");
        System.out.println(a+2);
        double b = Double.parseDouble("12.25");
        System.out.println(b + 0.25);

        System.out.println("==========================");

        //大小写转换
        String s1 = "abcdefg";
        System.out.println(s1.toUpperCase());
        String s2 = "ACBFED";
        System.out.println(s2.toLowerCase());

        System.out.println("==========================");

        //字符串转数组
        char[] chars1 = str1.toCharArray();
        System.out.println(Arrays.toString(chars1));

        System.out.println("==========================");

        //格式化
        String ret = String.format("%d-%d-%d",2025,6,15);
        System.out.println(ret);

        System.out.println("==========================");

        //替换字符
        //并不是将原来的字符串改变，而是返回一个新的字符串
        System.out.println(str1.replace('l', 'w'));
        System.out.println(str1.replace("el", "oo"));
        System.out.println(str1.replaceAll("el", "lolo"));
        System.out.println(str1.replaceFirst("l", "o"));

        System.out.println("==========================");

        //拆分字符串
        String str = "zhangsan&wangwu&lisi&zhaoliu";
        String[] strings = str.split("&");
        String[] strings1 = str.split("&",3);
        System.out.println(Arrays.toString(strings));
        System.out.println(Arrays.toString(strings1));

        //多个分割符
        String str0 = "name=zhangsan&age=18";
        String[] strings2 = str0.split("=|&");
        System.out.println(Arrays.toString(strings2));

        System.out.println("==========================");

        //字符的截取
        String str20 = "helloworld";
        //从哪个位置开始截取
        String str21 = str20.substring(3);
        System.out.println(str21);
        //截取的区间，遵循左闭右开区间
        String str22 = str20.substring(3,7);
        System.out.println(str22);

        System.out.println("==========================");

        //去掉字符串两边的空格
        String str23 = "  hello hello     ";
        String str25 = "world";
        System.out.println(str23+str25);
        String str24 = str23.trim();
        System.out.println(str24+str25);
    }
}
